package de.ipk_gatersleben.bit.bi.edal.publication.listener;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.LinkOption;

import javax.swing.JFileChooser;

import de.ipk_gatersleben.bit.bi.edal.publication.AttributeTextArea;
import de.ipk_gatersleben.bit.bi.edal.publication.DateTimePicker;
import de.ipk_gatersleben.bit.bi.edal.publication.DateTimePicker.DateChooser;
import de.ipk_gatersleben.bit.bi.edal.publication.PropertyLoader;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationFrame;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationMainPanel;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationModul;

public class NonFreeTextPanelMouseAdapter extends MouseAdapter {

	public enum PanelType {
		AUTHOR_PANEL, PUBLISHER_PANEL, LANGUAGE_PANEL, SUBJECT_PANEL, UPLOAD_PANEL, EMBARGO_PANEL;
	}

	private PanelType panelType;
	private int rowNumber = 0;

	public NonFreeTextPanelMouseAdapter(PanelType panelType, int rowNumber) {
		this.panelType = panelType;
		this.rowNumber = rowNumber;
	}

	@Override
	public void mousePressed(MouseEvent e) {
		this.mouseClicked(e);
	}

	@Override
	public void mouseClicked(MouseEvent mouseEvent) {

		switch (this.panelType) {

		case AUTHOR_PANEL:
			PublicationMainPanel.blockForAuthorsField();
			PublicationMainPanel.getMainPanel().remove(this.rowNumber);
			PublicationMainPanel.getMainPanel().add(
					PublicationMainPanel.authorPanel, this.rowNumber);
			break;

		case PUBLISHER_PANEL:
			PublicationMainPanel.blockForPublisherField();
			PublicationMainPanel.getMainPanel().remove(this.rowNumber);
			PublicationMainPanel.getMainPanel().add(
					PublicationMainPanel.publisherPanel, this.rowNumber);
			break;

		case LANGUAGE_PANEL:
			PublicationMainPanel.blockForLanguageField();
			PublicationMainPanel.getMainPanel().remove(this.rowNumber);
			PublicationMainPanel.getMainPanel().add(
					PublicationMainPanel.languagePanel, this.rowNumber);
			break;

		case SUBJECT_PANEL:
			PublicationMainPanel.blockForSubjectsField();
			PublicationMainPanel.getMainPanel().remove(this.rowNumber);
			PublicationMainPanel.getMainPanel().add(
					PublicationMainPanel.subjectPanel, this.rowNumber);
			break;

		case UPLOAD_PANEL:
			PropertyLoader.UPLOADPATH_LABEL
					.setForeground(PropertyLoader.LABEL_COLOR);

			AttributeTextArea focusedField = (AttributeTextArea) mouseEvent
					.getComponent();

			String dir = "";
			final Component parent = PublicationModul.getFrame();
			final JFileChooser chooser = new JFileChooser(dir);

			chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
				dir = chooser.getSelectedFile().getAbsolutePath();
				if (dir != null) {
					if (Files.exists(chooser.getSelectedFile().toPath(),
							LinkOption.NOFOLLOW_LINKS)) {
						focusedField.setText(dir);
						focusedField.setToolTipText(dir);
					}
				}
			}

			PropertyLoader.UPLOADPATH_LABEL
					.setForeground(PropertyLoader.LABEL_COLOR);
			break;
		case EMBARGO_PANEL:
			
//			try {
//				DateTimePicker.DateChooser dateChooser = (DateChooser) DateTimePicker.DateChooser.class.getConstructors()[0].newInstance();
//				
//			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
	
				DateTimePicker picker = new DateTimePicker(null);
				
				DateTimePicker.DateChooser chooser2 = picker.new DateChooser();
				Point p = chooser2.getLocationOnScreen();
				chooser2.showDateChooser(p);
			
			break;
			
		default:
			break;
		}
		PublicationFrame.updateUI();

	}
}
